package cn.stylefeng.guns.modular.rest;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.sht.entity.History;
import cn.stylefeng.guns.modular.sht.entity.StudyHistory;
import cn.stylefeng.guns.modular.sht.model.params.HistoryParam;
import cn.stylefeng.guns.modular.sht.model.params.QuestionParam;
import cn.stylefeng.guns.modular.sht.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.sht.model.result.HistoryResult;
import cn.stylefeng.guns.modular.sht.model.result.StudyHistoryResult;
import cn.stylefeng.guns.modular.sht.service.HistoryService;
import cn.stylefeng.guns.modular.sht.service.QuestionService;
import cn.stylefeng.guns.modular.sht.service.ShtStudyHistoryService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 学习历史控制器
 *
 * @author
 * @Date 2021-01-22 20:22:51
 */
@RestController
@RequestMapping("/miniprogramApi/history")
public class ApiHistoryController extends BaseController {

    @Autowired
    private HistoryService historyService;
    @Autowired
    private ShtStudyHistoryService studyHistoryService;
    @Autowired
    private QuestionService questionService;

    /**
     * 新增接口
     *
     * @author
     * @Date 2021-01-22
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HistoryParam historyParam, Integer okCnt, Integer ngCnt) {
        this.historyService.add(historyParam);

        // 更新答题总览(对、错、总数)
        StudyHistoryParam param = new StudyHistoryParam();
        param.setGroupTime(historyParam.getGroupTime());
        StudyHistoryResult studyHistoryResult = studyHistoryService.findBySpec(param);
        StudyHistory studyHistory = new StudyHistory();
        ToolUtil.copyProperties(studyHistoryResult, studyHistory);
        studyHistory.setCurrect(okCnt);
        studyHistory.setWrong(ngCnt);
        QuestionParam questionParam = new QuestionParam();
        questionParam.setQuestionBankId(historyParam.getQuestionBankId());
        int total = questionService.cntTotal(questionParam);
        studyHistory.setTotal(total);
        studyHistoryService.updateById(studyHistory);

        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2021-01-22
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HistoryParam historyParam) {
        this.historyService.update(historyParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2021-01-22
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HistoryParam historyParam) {
        this.historyService.delete(historyParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2021-01-22
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HistoryParam historyParam) {
        History detail = this.historyService.getById(historyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2021-01-22
     */
    @ResponseBody
    @RequestMapping("/list")
    public ResponseData list(HistoryParam historyParam) {
        List<HistoryResult> listBySpec = this.historyService.findListBySpec(historyParam);
        return ResponseData.success(listBySpec);
    }

    // 查找当前用户所有错题
    @ResponseBody
    @RequestMapping("/wrong/list")
    public ResponseData listPage(HistoryParam historyParam) {
        LayuiPageInfo listBySpec = this.historyService.wrongList(historyParam);
        return ResponseData.success(listBySpec);
    }
}


